package com.hefu.virtuallife.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hefu.virtuallife.R;
import com.hefu.virtuallife.bean.SmartHomeBean;
import com.hefu.virtuallife.customview.ablgrid.AblGridView;
import com.hefu.virtuallife.customview.ablgrid.AblGridViewBaseAdapter;
import com.hefu.virtuallife.customview.ablgrid.AblGridViewBaseAdapter1;
import com.hefu.virtuallife.utils.SmartDataUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmartSceneActivity extends AppCompatActivity implements View.OnClickListener{

    @BindView(R.id.img_back)
    ImageView back_btn;
    @BindView(R.id.img_home)
    ImageView img_home;

    @BindView(R.id.hsl_scene)
    HorizontalScrollView hsl_scene;
    @BindView(R.id.gv_scene)
    AblGridView ablGridView;


    private String[] scenes = {"结婚纪念日","回家","开会","聚会"};
    private int[] sceneImgs = {R.drawable.scene_memorialday_bg, R.drawable.scene_home_bg, R.drawable.scene_work_bg, R.drawable.scene_party_bg};
    int[] smartImgs = {R.drawable.scene_memorialday, R.drawable.scene_home, R.drawable.scene_work, R.drawable.scene_party};
    private Map<String, List<SmartHomeBean>> sceneMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_scene);

        ButterKnife.bind(this);

        back_btn.setOnClickListener(this);
        img_home.setOnClickListener(this);

        getData();
        initGridView();
    }

    private void getData(){
        for (String scene : scenes){
            sceneMap.put(scene, SmartDataUtils.querySceneEquip(scene));
        }
    }

    private void initGridView() {
        int size = scenes.length;
        int length = 220;

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float density = metrics.density;

        RelativeLayout.LayoutParams rlparams = (RelativeLayout.LayoutParams)hsl_scene.getLayoutParams();
        rlparams.topMargin = metrics.heightPixels * 3  /  7;
        //rlparams.bottomMargin = screenHeight / 3;
        hsl_scene.setLayoutParams(rlparams);


        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
        ablGridView.setLayoutParams(params);
        ablGridView.setColumnWidth(itemWidth); // 设置列表项宽
        ablGridView.setHorizontalSpacing(5); // 设置列表项水平间距
        ablGridView.setStretchMode(GridView.NO_STRETCH);
        ablGridView.setNumColumns(size);


        AblGridViewBaseAdapter adapter = new AblGridViewBaseAdapter(this, scenes, sceneImgs, smartImgs);
        ablGridView.setAdapter(adapter);

        ablGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position >= 0 && position<scenes.length){
                    String scene = scenes[position];
                    int imgId = sceneImgs[position];
                    Intent intent = new Intent(SmartSceneActivity.this, SmartSceneSettingsActivity.class);
                    intent.putExtra("scene", scene);
                    intent.putExtra("imgId", imgId);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.img_home:
                Intent intent = new Intent(SmartSceneActivity.this, SmartPersonalActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
